<?php

namespace App\Models;

use Jenssegers\Mongodb\Eloquent\Model as Moloquent;
use Jenssegers\Mongodb\Eloquent\SoftDeletes;
use App\Http\Lib\UserLib;

class User extends Moloquent
{
    use SoftDeletes;
    
    protected $collection = 'users';
    protected $connection = 'mongodb';
    
    protected $hidden = [
        '__v', 'password', 'api_token',
    ];
    
    protected $guarded = [];
        
    protected $dates = ['deleted_at'];
    
    public function save(array $options = []){
        if(!parent::save($options))
            return false;
    
        // 如果修改的是当前登录用户的信息，得即时更新登录用户信息
        $cur = UserLib::getLoginUser();
        if($cur && $cur->id === $this->id){
            $cur->setModel($this);
        }
    
        return true;
    }
    
    public function delete(){
        $this->email .= '@del' . time();
        $this->save();
    
        return parent::delete();
    }

}